package com.qf.repo.controller;

import com.alibaba.fastjson.JSON;
import com.qf.repo.bean.Manager;
import com.qf.repo.bean.RespResult;
import com.qf.repo.common.Const;
import com.qf.repo.dao.Impl.ManagerDaoImpl;
import com.qf.repo.dao.ManagerDao;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * projectName:CangKu_guanli
 * author:LiPengChong
 * time:2024/5/9 16:25
 * description:
 */
@WebServlet("/manager")
public class ManagerServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setCharacterEncoding("UTF-8");

        String method = request.getParameter("method");
        //后台管理员功能有：login(登录)、logout(退出)、modify_permission(修改权限).....等
        RespResult result = null;
        switch (method) {//没用反射才用的switch
            case "login":
                result = login(request, response);
                break;
            default:
                break;
        }
        PrintWriter writer = response.getWriter();
        writer.write(JSON.toJSONString(result));
        writer.flush();
        writer.close();
    }

    /**
     * 后台管理员的登录方法
     *
     * @param request  请求
     * @param response 响应
     * @throws ServletException
     * @throws IOException
     */
    public RespResult login(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //1.获取前端数据
        String account = request.getParameter("account");
        String password = request.getParameter("password");
        System.out.println(account + "," + password);

        //1.2参数的合法性验证
        RespResult result = new RespResult();
        if (account.length() < 4 || account.length() > 12) {
            result.setMsg("参数不合法");
            result.setCode(Const.LOGIN_NOTEXIST);
            return result;
        }
        //2.调用业务逻辑
        ManagerDao managerDao = new ManagerDaoImpl();
        Manager manager = managerDao.login(account, password);
        //3.判断结果

        if (manager == null) {
            //返回 登陆失败结果
            //“登陆失败”
            result.setCode(Const.LOGIN_FAIT);
            result.setMsg("登陆失败");
        } else {
            //返回 登录成功的结果
            result.setCode(Const.LOGIN_SUCCESS);
            result.setMsg("success");
            //manager对象返回
            //fastjson、gosn(json序列化使用的两种json)

            result.setData(JSON.toJSONString(manager));
        }
        //4.返回前端
//        writer.write(JSON.toJSONString(result));
//        writer.flush();
//        writer.close();
        return result;
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doGet(request, response);
    }
}


